﻿using Corina.DataContext.Service;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Master
{
    public class UserHypotaxisMaster : MasterBase
    {
        public ResultInfo GetSubusers(string account, string password, int userId, int index, int size)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                int count = 0;
                var table = GetInstance<UserHypotaxisService>().GetSubusers(ConnectionString(DatabaseKeys.CORINA_ACCO), userId, index, size, ref count);
                string[] columns = table.Columns.OfType<DataColumn>().Select(t => t.ColumnName).ToArray();

                var items = (from t in table.AsEnumerable()
                             select new DataObjectInfo()
                             {
                                 Values = GetObjectOfValue(t, columns)
                             });

                result.DataObjects.AddRange(items);
                result.TotalCount = count;
            }

            return result;
        }

        public ResultInfo GetSubusers(string account, string password, int index, int size)
        {
            ResultValue resultValue = VerifyAccount<UserInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                int userId = GetAccountInfo<UserInfo>(account).Id;

                int count = 0;
                var table = GetInstance<UserHypotaxisService>().GetSubusers(ConnectionString(DatabaseKeys.CORINA_ACCO), userId, index, size, ref count);
                string[] columns = table.Columns.OfType<DataColumn>().Select(t => t.ColumnName).ToArray();

                var items = (from t in table.AsEnumerable()
                             select new DataObjectInfo()
                             {
                                 Values = GetObjectOfValue(t, columns)
                             });

                result.DataObjects.AddRange(items);
                result.TotalCount = count;
            }

            return result;
        }

        public ResultInfo GetUnallocateUsers(string account, string password, string key, int index, int size)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                int count = 0;
                var table = GetInstance<UserHypotaxisService>().GetUnallocateUsers(ConnectionString(DatabaseKeys.CORINA_ACCO), key, index, size, ref count);
                string[] columns = table.Columns.OfType<DataColumn>().Select(t => t.ColumnName).ToArray();

                var items = (from t in table.AsEnumerable()
                             select new DataObjectInfo()
                             {
                                 Values = GetObjectOfValue(t, columns)
                             });

                result.DataObjects.AddRange(items);
                result.TotalCount = count;
            }

            return result;
        }

        public ResultInfo Update(string account, string password, int superiorId, string subids)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);
            if (resultValue == ResultValue.Succeed)
            {
                IEnumerable<int> ids = null;
                try
                {
                    ids = subids.Split(',').Select(t => Convert.ToInt32(t));
                }
                catch
                {
                    resultValue = ResultValue.DataFormatError;
                }
                if (ids != null && ids.Count() > 0)
                {
                    bool flag = GetInstance<UserHypotaxisService>().Update(ConnectionString(DatabaseKeys.CORINA_ACCO), superiorId, ids.ToArray());
                    if (!flag)
                    {
                        resultValue = ResultValue.Failed;
                    }
                }
            }

            return new ResultInfo(resultValue);
        }
    }
}
